﻿using FTBS.Models.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web;

namespace FTBS.Models.DAO
{
    public class ChairDAO
    {
        private FilmTicketBookingSystemEntities _entities = new FilmTicketBookingSystemEntities();

        /// <summary>
        /// Load all Chairs by RoomId
        /// </summary>
        /// <param name="roomId">RoomId</param>
        /// <returns>List of Chair</returns>
        /// <author>VuongVT</author>
        public List<Chair> LoadChairs(int roomId)
        {
            List<Chair> listChair = new List<Chair>();
            try
            {
                var resultQuery = from chairs in _entities.Chairs
                                  where chairs.RoomId == roomId
                                  select chairs;
                if (resultQuery != null)
                {
                    listChair = resultQuery.ToList();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return listChair;
        }

        /// <summary>
        /// Get Chair by ChairId
        /// </summary>
        /// <param name="id">ChairId</param>
        /// <returns>Chair</returns>
        /// <author>VuongVT</author>
        public Chair GetChairById(int id)
        {
            Chair chairEnt = new Chair();
            try
            {
                chairEnt = (from chair in _entities.Chairs
                            where chair.ChairId == id
                                  && chair.IsDeleted == false
                            select chair).FirstOrDefault();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return chairEnt;
        }

        /// <summary>
        /// load chairs with isdeleted = false
        /// </summary>
        /// <param name="roomId"></param>
        /// <returns>list of chairs</returns>
        /// <author>hungph</author>
        public List<Chair> LoadAllChairs(int roomId)
        {
            List<Chair> listChair = new List<Chair>();
            try
            {
                var resultQuery = from chairs in _entities.Chairs
                                  where chairs.RoomId == roomId && chairs.IsDeleted == false
                                  select chairs;
                if (resultQuery != null)
                {
                    listChair = resultQuery.ToList();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return listChair;
        }

        /// <summary>
        /// Get Chair by ChairId
        /// </summary>
        /// <param name="id">ChairId</param>
        /// <returns>Chair</returns>
        /// <author>hungph</author>
        public Chair GetChairByIdWithoutCheckingIsDeleted(int id)
        {
            Chair chairEnt = new Chair();
            try
            {
                chairEnt = (from chair in _entities.Chairs
                            where chair.ChairId == id
                            select chair).FirstOrDefault();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return chairEnt;
        }

        ///// <summary>
        ///// Update status Booked for chair
        ///// </summary>
        ///// <returns>1: success -- 0: fail</returns>
        ///// <param name="id">ChairId</param>
        ///// <author>VuongVT</author>
        //public int UpdateChair(int id)
        //{
        //    try
        //    {
        //        Chair chair = new Chair();
        //        chair = GetChairById(id);
        //        var resultQuery = _entities.SaveChanges();
        //        return resultQuery;
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }

        //}

        /// <summary>
        /// them 1 ghe vao phong
        /// </summary>
        /// <param name="chair"></param>
        /// <param name="id"></param>
        /// <param name="chairType"></param>
        /// <author>nguyenht</author>
        public bool AddChair(string[] strChair, int id, string[] strChairType, string[] AstrChair)
        {
            using (TransactionScope trans = new TransactionScope())
            {
                try
                {
                    for (int i = 0; i < AstrChair.Length; i++)
                    {
                        Chair chairs = new Chair();
                        chairs.ChairPosition = AstrChair[i];
                        chairs.ChairTypeId = 1;
                        chairs.RoomId = id;
                        chairs.IsDeleted = true;
                        _entities.Chairs.Add(chairs);
                        for (int j = 0; j < strChair.Length; j++)
                        {
                            if (strChair[j] == AstrChair[i])
                            {
                                chairs.IsDeleted = false;
                                chairs.ChairTypeId = Convert.ToInt32(strChairType[j]);
                            }

                        }
                        _entities.SaveChanges();
                    }
                    trans.Complete();
                    return true;
                }

                catch
                {
                    trans.Dispose();
                    return false;
                }

            }
        }

        public bool Edit(string[] strChair, int id, string[] strChairType)
        {
            using (TransactionScope trans = new TransactionScope())
            {
                try
                {
                    var resultQuery = _entities.Chairs.Where(s => s.RoomId == id);
                    foreach (var item in resultQuery)
                    {
                        item.IsDeleted = true;
                        for (int j = 0; j < strChair.Length; j++)
                        {
                            if (item.ChairPosition == strChair[j])
                            {
                                item.IsDeleted = false;
                                item.ChairTypeId = Convert.ToInt32(strChairType[j]);
                            }
                        }
                    }
                    _entities.SaveChanges();
                    trans.Complete();
                    return true;
                }
                catch 
                {
                    trans.Dispose();
                    return false;
                }
            }
        }
       
        public int DeleteChairById(Room room)
        {
            var id = room.RoomId;
            try
            {
                var resultQuery = _entities.Chairs.Where(s => s.RoomId == id);
                foreach (var item in resultQuery)
                {
                    item.IsDeleted = true;
                }
                _entities.SaveChanges();
                return 1;
            }
            catch { return 0; }

        }
       
    }
}